package com.flute.icrawler.framework.processor.result;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.log4j.Logger;

import com.flute.icrawler.framework.framework.CrawlUrl;
import com.flute.icrawler.framework.framework.ICrawlUrlPool;

public class AddUrlProcessorResult extends AbstractProcessResult {

	private static Logger logger = Logger
			.getLogger(AddUrlProcessorResult.class);

	/**
	 * 
	 */
	private static final long serialVersionUID = -3443866817326167075L;

	private HashSet<String> urlSet;
	private List<CrawlUrl> urlList;

	public AddUrlProcessorResult() {
		this.urlSet = new HashSet<String>();
		this.urlList = new ArrayList<CrawlUrl>();
	}

	@Override
	public void handle(CrawlUrl url, ICrawlUrlPool pool) {

		try {
			long time = System.currentTimeMillis();
			logger.info("urlList size:"+urlList.size());

			pool.addAll(urlList);
			logger.info("AddUrlProcessorResult handle time:"
					+ (System.currentTimeMillis() - time) + "ms");
		} catch (RemoteException e) {
			e.printStackTrace();
		}

	}

	public void addUrl(CrawlUrl url) {
		if (!urlSet.contains(url.getUrl())) {
			urlList.add(url);
			urlSet.add(url.getUrl());
		}
	}

	@Override
	public boolean isNeedInterrupt() {
		return false;
	}

	@Override
	public boolean isUnique() {
		return false;
	}
}
